<?php
namespace app\index\relevan;
use app\index\controller\Common;
use app\index\model\DealbianListModel;
use app\index\model\DealcomListModel;
use app\index\model\DealcomModel;
use app\index\model\DeviceJinListModel;
use app\index\model\DeviceTuiListModel;
use app\index\model\DeviceYunListModel;

class OtherRelevan extends Common
{

    public function vate_dealbian_info($minfo){
        $dealinfo = [];
        $dealinfo['name'] = $minfo['name'];
        $dealinfo['type'] = $minfo['type'];
        $dealinfo['nature'] = $minfo['nature'];
        $dealinfo['legaler'] = $minfo['legaler'];
        $dealinfo['lianaddr'] = $minfo['lianaddr'];
        $dealinfo['lianer'] = $minfo['lianer'];
        $dealinfo['liantel'] = $minfo['liantel'];
        $dealinfo['zhuwu'] = $minfo['zhuwu'];
        $dealinfo['explain'] = $minfo['explain'];
        $dealinfo['taxtype'] = $minfo['taxtype'];
        $dealinfo['piaotype'] = $minfo['piaotype'];
        $dealinfo['ratenum'] = $minfo['ratenum'];
        $dealinfo['rate'] = $minfo['rate'];
        $dealinfo['unitaddr'] = $minfo['unitaddr'];
        $dealinfo['unittel'] = $minfo['unittel'];
        $dealinfo['unitbkname'] = $minfo['unitbkname'];
        $dealinfo['unitbknum'] = $minfo['unitbknum'];
        $dealinfo['banknum'] = $minfo['banknum'];
        $dealinfo['bankname'] = $minfo['bankname'];
        DealcomModel::where(['id'=>$minfo['mid']])->update($dealinfo);

        DealcomListModel::where(['dealid'=>$minfo['mid']])->delete();
        $bianlist = DealbianListModel::where(['dealid'=>$minfo['id']])->select()->toArray();
        foreach ($bianlist as $vvv){
            unset($vvv['id']);
            $vvv['dealid'] = $minfo['mid'];
            DealcomListModel::insert($vvv);
        }

        return true;
    }


    public function vate_device_yun_info($minfo){
        $yunlist = DeviceYunListModel::where(['yunid'=>$minfo['id']])->select()->toArray();
        foreach ($yunlist as $item){
            $yunhour = DeviceYunListModel::alias('list')
                ->join('device_yun yun','yun.id=list.yunid')
                ->where(['yun.deleted_user'=>0, 'yun.isturn'=>1, 'yun.status'=>1])
                ->where(['yun.enginid'=>$minfo['enginid'], 'list.listid'=>$item['listid']])
                ->sum('list.yunhour');

            $yununit = DeviceYunListModel::alias('list')
                ->join('device_yun yun','yun.id=list.yunid')
                ->where(['yun.deleted_user'=>0, 'yun.isturn'=>1, 'yun.status'=>1])
                ->where(['yun.enginid'=>$minfo['enginid'], 'list.listid'=>$item['listid']])
                ->sum('list.yununit');
            DeviceJinListModel::where(['id'=>$item['listid']])->update([
                'yunhour' => $yunhour,
                'yununit' => $yununit,
            ]);
        }

        return true;
    }
    public function vate_device_tui_info($minfo){
        $tuilist = DeviceTuiListModel::where(['tuiid'=>$minfo['id']])->select()->toArray();
        foreach ($tuilist as $item){
            $tuidt = DeviceTuiListModel::alias('list')
                ->join('device_tui tui','tui.id=list.tuiid')
                ->where(['tui.deleted_user'=>0, 'tui.isturn'=>1, 'tui.status'=>1])
                ->where(['tui.enginid'=>$minfo['enginid'], 'list.listid'=>$item['listid']])
                ->value('list.tuidt');
            if($tuidt){
                $state = '退场';
            }else{
                $state = '在场';
            }

            DeviceJinListModel::where(['id'=>$item['listid']])->update([
                'tuidt' => $tuidt,
                'state' => $state,
            ]);
        }

        return true;
    }


}
